<?php

namespace Home\Service;

class ShitTimeService extends CommonService {

    public function getRecentShitTime($userId, $begin, $end, $days) {
        $ShitTime = M('ShitTime');

        $where = array(
            'created_at' => array(array('elt', $end), array('egt', $begin)),
            'user_id' => $userId
        );
        $shitTimes = $ShitTime->where($where)->order('id DESC')->select();

        $oneDaySeconds = 86400;
        $timestamp = time() + $oneDaySeconds;
        $dateKeys = array();
        $durations = array();
        for ($i = $days; $i > 0; $i--) {
            $dateKey = date('d', $timestamp - $oneDaySeconds * $i);
            $dateKeys[] = $dateKey;
            $durations[$dateKey] = 0;
        }

        foreach ($shitTimes as $shitTime) {
            $dateKey = date('d', $shitTime['created_at']);
            $durations[$dateKey] = $shitTime['duration'];
        }

        return array('dateKeys' => $dateKeys, 'durations' => array_values($durations));
    }

    protected function getModelName() {
        return 'ShitTime';
    }
}
